package com.sitech.ibnms.util;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import javax.naming.NamingException;
import javax.naming.directory.Attribute;

/**
 * 
 * <p>
 * Title: ValidatorUtil
 * </p>
 * <p>
 * Description: 操作验证工具类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2010
 * </p>
 * <p>
 * Company: SI-TECH
 * </p>
 * 
 * @author xuyw
 * @version 1.0
 * @createtime Sep 25, 2010 10:30:37 AM
 * 
 */
public class ValidatorUtil
{

	/**
	 * 判断两个字符串是否相等
	 * 
	 * @param src
	 *            源字符串
	 * @param des
	 *            目标字符串
	 * @return {true: 相等；false：不相等}
	 */
	public static boolean equals(String src, String des)
	{
		if ((src == null) && (des == null))
		{
			return true;
		}
		else if ((src == null) || (des == null))
		{
			return false;
		}
		else
		{
			return src.equals(des);
		}
	}

	/**
	 * 是否为字符
	 * 
	 * @param c
	 *            字符
	 * @return {true：字符；false：非字符}
	 */
	public static boolean isChar(char c)
	{
		return Character.isLetter(c);
	}

	/**
	 * 判断字符串是否由字符组成
	 * 
	 * @param str
	 *            原始字符串
	 * @return {true：字符；false：非字符}
	 */
	public static boolean isChar(String str)
	{
		if (isNull(str))
		{
			return false;
		}
		char[] c = str.toCharArray();
		for (int i = 0; i < c.length; i++)
		{
			if (!isChar(c[i]))
			{
				return false;
			}
		}
		return true;
	}

	/**
	 * 是否为数字字符
	 * 
	 * @param c
	 *            原始字符
	 * @return {true：数字；false：非数字}
	 */
	public static boolean isDigit(char c)
	{
		int x = (int) c;
		if ((x >= 48) && (x <= 57))
		{
			return true;
		}
		return false;
	}

	/**
	 * 字符串是否由数字组成
	 * 
	 * @param number
	 *            原始字符串
	 * @return {true：数字；false：非数字}
	 */
	public static boolean isDigit(String number)
	{
		if (isNull(number))
		{
			return false;
		}
		char[] c = number.toCharArray();
		for (int i = 0; i < c.length; i++)
		{
			if (!isDigit(c[i]))
			{
				return false;
			}
		}
		return true;
	}

	/**
	 * 字符串为否为空
	 * 
	 * @param str
	 *            原始字符串
	 * @return {true：空；false：非空}
	 */
	public static boolean isNull(String str)
	{
		if (str == null)
		{
			return true;
		}
		str = str.trim();
		if ((str.equals(StringConst.NULL)) || (str.equals(StringConst.BLANK)))
		{
			return true;
		}
		return false;
	}

	/**
	 * attribute为否为空
	 * 
	 * @param str
	 *            原始attribute
	 * @return {true：空；false：非空}
	 */
	public static boolean isNull(Attribute attribute)
	{
		if (attribute == null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * Retrieves one of this attribute's values. If the attribute has more than
	 * one value and is unordered, any one of the values is returned. If the
	 * attribute has more than one value and is ordered, the first value is
	 * returned.
	 * 
	 * @param str
	 *            原始attribute
	 * @return string
	 */
	public static String parseString(Attribute attribute) throws NamingException
	{
		if (isNull(attribute))
		{
			return "";
		}
		else
		{
			return (String) attribute.get();
		}
	}

	/**
	 * parseStringList 处理多值
	 * 
	 * @param 原始attribute
	 * @return attribute的值组成的list
	 * @throws NamingException
	 * 
	 * @author jiayb Jul 10, 2009 11:48:21 AM
	 */
	@SuppressWarnings("unchecked")
	public static List parseStringList(Attribute attribute) throws NamingException
	{
		if (isNull(attribute))
		{
			return null;
		}
		else
		{
			Enumeration vals = attribute.getAll();
			ArrayList valList = new ArrayList();
			while (vals.hasMoreElements())
			{
				valList.add(vals.nextElement());
			}
			return valList;
		}
	}

	/**
	 * 是否为html格式的字符串
	 * 
	 * @param s
	 *            原始字符串
	 * @return {true：是；false：否}
	 */
	public static boolean isHTML(String s)
	{
		if (isNull(s))
		{
			return false;
		}
		if (((s.indexOf("<html>") != -1) || (s.indexOf("<HTML>") != -1))
				&& ((s.indexOf("</html>") != -1) || (s.indexOf("</HTML>") != -1)))
		{
			return true;
		}
		return false;
	}

	/**
	 * 是否为Email地址
	 * 
	 * @param ea
	 *            原始String
	 * @return {true：是；false：否}
	 */
	public static boolean isEmailAddress(String ea)
	{
		if (isNull(ea))
		{
			return false;
		}
		int eaLength = ea.length();
		if (eaLength < 6)
		{
			// j@j.c
			return false;
		}
		ea = ea.toLowerCase();
		int at = ea.indexOf('@');
		// Unix based email addresses cannot be longer than 24 characters.
		// However, many Windows based email addresses can be longer than 24,
		// so we will set the maximum at 48.
		// int maxEmailLength = 24;
		int maxEmailLength = 48;
		if ((at > maxEmailLength) || (at == -1) || (at == 0) || ((at <= eaLength) && (at > eaLength - 5)))
		{

			return false;
		}
		int dot = ea.lastIndexOf('.');
		if ((dot == -1) || (dot < at) || (dot > eaLength - 3))
		{
			// joe@joecom
			// joe.@joecom
			// joe@joe.c
			return false;
		}
		if (ea.indexOf("..") != -1)
		{
			return false;
		}
		char[] name = ea.substring(0, at).toCharArray();
		for (int i = 0; i < name.length; i++)
		{
			if ((!isChar(name[i])) && (!isDigit(name[i])) && (name[i] != '.') && (name[i] != '-') && (name[i] != '_'))
			{
				return false;
			}
		}
		if ((name[0] == '.') || (name[name.length - 1] == '.') || (name[0] == '-') || (name[name.length - 1] == '-')
				|| (name[0] == '_'))
		{ // || (name[name.length - 1] == '_')) {
			return false;
		}
		char[] host = ea.substring(at + 1, ea.length()).toCharArray();
		for (int i = 0; i < host.length; i++)
		{
			if ((!isChar(host[i])) && (!isDigit(host[i])) && (host[i] != '.') && (host[i] != '-'))
			{
				return false;
			}
		}
		if ((host[0] == '.') || (host[host.length - 1] == '.') || (host[0] == '-') || (host[host.length - 1] == '-'))
		{
			return false;
		}
		if (ea.startsWith("postmaster@"))
		{
			return false;
		}
		if (ea.startsWith("root@"))
		{
			return false;
		}
		return true;
	}

	/**
	 * 判断字符串是否不为空
	 * 
	 * @param s
	 * @return {true：非空；false：空}
	 */
	public static boolean isNotNull(String s)
	{
		return !isNull(s);
	}

	/**
	 * 是否为只含有数字和字符的密码
	 * 
	 * @param passwd
	 *            原始string
	 * @return {true：是；false：否}
	 */
	public static boolean isPassword(String passwd)
	{
		if (isNull(passwd))
		{
			return false;
		}
		if (passwd.length() < 4)
		{
			return false;
		}
		char[] c = passwd.toCharArray();
		for (int i = 0; i < c.length; i++)
		{
			if ((!isChar(c[i])) && (!isDigit(c[i])))
			{
				return false;
			}
		}
		return true;
	}

	/**
	 * 判断数组中是否存在指定元素
	 * 
	 * @param array
	 *            原始数组
	 * @param element
	 *            原始元素
	 * @return {true：是；false：否}
	 */
	public static boolean isContain(String[] array, String element)
	{
		int size = array.length;
		for (int i = 0; i < size; i++)
		{
			if (array[i].equalsIgnoreCase(element))
			{
				return true;
			}
		}
		return false;
	}

	/**
	 * 判断列表是否为空
	 * 
	 * @param list
	 * @return {true：是；false：否}
	 */
	@SuppressWarnings("unchecked")
	public static boolean isNullList(List list)
	{
		if ((list == null) || (list.size() == 0))
		{
			return true;
		}
		return false;
	}

	/**
	 * 判断列表是否不为空
	 * 
	 * @param list
	 * @return @return {true：非空；false：空}
	 */
	@SuppressWarnings("unchecked")
	public static boolean isNotNullList(List list)
	{
		return !isNullList(list);
	}

	/**
	 * 判断列表是否包含某元素，是否忽略大小写
	 * 
	 * @param list
	 *            列表
	 * @param element
	 *            元素名
	 * @param ignoreCase
	 *            是否忽略大小写
	 * @return {true：是；false：否}
	 */
	@SuppressWarnings("unchecked")
	public static boolean isContain(List list, String element, boolean ignoreCase)
	{

		if (ignoreCase)
		{
			for (Iterator iterator = list.iterator(); iterator.hasNext();)
			{
				String object = (String) iterator.next();
				if (object.equalsIgnoreCase(element))
				{
					return true;
				}
			}
		}
		else
		{
			return list.contains(element);
		}
		return false;
	}
}
